using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using UltiCoreLibrary;

namespace UltiSheet
{
    public partial class Contra : Form
    {

        private GameState gameState;

        private Boolean colored = false;

        private Party party;

        public GameState GameState
        {
            // get { return gameState; }
            set { gameState = value; }
        }

        private Dictionary<ComboBox, BasicGame> gameCombos = new Dictionary<ComboBox, BasicGame>();

        public Contra()
        {
            InitializeComponent();
        }

        private void Contra_Activated(object sender, EventArgs e)
        {
            party = gameState.parties.Last.Value;
            if (party.Game.Trump.Color == GameColors.COLOR_COLORLESS)
            {
                colored = false;
                labelContraDefender1.Text = party.Defender1.Name;
                labelContraDefender2.Text = party.Defender2.Name;

                foreach (ContraLevel level in ContraLevel.contraLevels)
                {
                    comboBoxDefender1.Items.Add(level);
                    comboBoxDefender2.Items.Add(level);
                }
                comboBoxDefender1.SelectedIndex = 0;
                comboBoxDefender2.SelectedIndex = 0;
            }
            else
            {
                colored = true;
                labelContraDefender1.Visible = false;
                labelContraDefender2.Visible = false;
                comboBoxDefender1.Visible = false;
                comboBoxDefender2.Visible = false;

                int i = 0;
                foreach (BasicGame subGame in party.Game.SubGames.Keys)
                {
                    Label label = new Label();
                    label.Location = new Point(5, 5 + i * 60);
                    label.Size = new Size(230, 20);
                    label.Anchor = AnchorStyles.Right | AnchorStyles.Left;
                    label.Text = subGame.ToString();
                    panelContra.Controls.Add(label);

                    ComboBox contra = new ComboBox();
                    contra.Location = new Point(5, 35 + i * 60);
                    contra.Size = new Size(230, 20);
                    contra.Anchor = AnchorStyles.Left | AnchorStyles.Right;
                    foreach (ContraLevel level in ContraLevel.contraLevels)
                    {
                        contra.Items.Add(level);
                    }
                    contra.SelectedIndex = 0;
                    panelContra.Controls.Add(contra);
                    gameCombos.Add(contra, subGame);
                    i++;
                }
            }
        }

        private void Contra_Closing(object sender, CancelEventArgs e)
        {
            if (colored)
            {
                foreach (ComboBox box in gameCombos.Keys)
                {
                    party.SetContra(gameCombos[box], (ContraLevel)box.SelectedItem);
                }

            }
            else
            {
                party.SetContra(
                    (ContraLevel)comboBoxDefender1.SelectedItem, 
                    (ContraLevel)comboBoxDefender2.SelectedItem
                    );
            }
        }
    }
}
